﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.Revit;
using YJKArchMethodLibrary.Utils;
using YRevitJig;
using YJKTextDimension.Utility;

namespace YJKDimensionModel.LevelDimension
{
	public class LevelDimensionManager
	{
		public LevelDimensionManager(ExternalCommandData cmdData)
		{
			this.m_cmdData = cmdData;
			this.m_doc = this.m_cmdData.Application.ActiveUIDocument.Document;
			this.m_view = cmdData.Application.ActiveUIDocument.ActiveView;
			this.m_nRevitNumber = Convert.ToInt32(this.m_cmdData.Application.Application.VersionNumber);
		}

		public void DrawLevelDimension(LevelDimensionForm dlgMain)
		{
			this.m_dlgMain = dlgMain;
			Reference reference = null;
			Element element = null;
			XYZ ptInsert = null;
			for (;;)
			{
				UIDocument activeUIDocument = this.m_cmdData.Application.ActiveUIDocument;
				if (dlgMain.m_bIsDlgClosed)
				{
					break;
				}
				using (LevelDimensionJig levelDimensionJig = new LevelDimensionJig(activeUIDocument, dlgMain))
				{
					if (dlgMain.Automatic)
					{
						if (dlgMain.Associate)
						{
							reference = levelDimensionJig.PickEdge("请选择插入点：");
							if (reference == null)
							{
								if (dlgMain.m_bIsLevelDlg)
								{
									continue;
								}
								return;
							}
						}
						else
						{
							element = levelDimensionJig.PickElement(out reference, "请选择实体：");
							if (element == null)
							{
								if (dlgMain.m_bIsLevelDlg)
								{
									continue;
								}
								return;
							}
							else
							{
								if (!this.m_bViewPlan)
								{
									levelDimensionJig.ResetPoint = XUiJigBase.EResetPoint.eByAngle;
									levelDimensionJig.LineAngle = 0.0;
								}
								XYZ xyz = levelDimensionJig.PickEndPoint(levelDimensionJig.StartPoint, "请确定直线终点：");
								if (xyz == null)
								{
									if (dlgMain.m_bIsLevelDlg)
									{
										continue;
									}
									return;
								}
								else
								{
									ptInsert = xyz;
									this.GetDrawOrientation(levelDimensionJig.StartPoint, xyz);
								}
							}
						}
					}
					else
					{
						XYZ xyz2 = levelDimensionJig.PickStartPoint("请确定直线起点：");
						if (xyz2 == null)
						{
							if (dlgMain.m_bIsLevelDlg)
							{
								continue;
							}
							return;
						}
						else
						{
							if (!this.m_bViewPlan)
							{
								levelDimensionJig.ResetPoint = XUiJigBase.EResetPoint.eByAngle;
								levelDimensionJig.LineAngle = 0.0;
							}
							XYZ xyz3 = levelDimensionJig.PickEndPoint(xyz2, "请确定直线终点：");
							if (xyz3 == null)
							{
								if (dlgMain.m_bIsLevelDlg)
								{
									continue;
								}
								return;
							}
							else
							{
								ptInsert = xyz3;
								this.GetDrawOrientation(levelDimensionJig.StartPoint, xyz3);
							}
						}
					}
				}
				dlgMain.m_bIsLevelDlg = false;
				this.CreateLevelDimension(ptInsert, reference, element);
			}
		}

		public bool ViewValidating()
		{
			if (typeof(ViewPlan) == this.m_view.GetType())
			{
				ViewPlan viewPlan = this.m_view as ViewPlan;
				if (viewPlan.SketchPlane == null)
				{
					Plane plane = RevitVersionFuncs.CreatePlanByOriginAndBasis(viewPlan.Origin, viewPlan.RightDirection, viewPlan.UpDirection);
					Transaction transaction = new Transaction(this.m_doc);
					transaction.Start("new sketchPlane");
					SketchPlane sketchPlane = SketchPlane.Create(this.m_doc, plane);
					this.m_view.SketchPlane = sketchPlane;
					transaction.Commit();
				}
				this.m_bViewPlan = true;
				return true;
			}
			if (typeof(ViewSection) == this.m_view.GetType())
			{
				ViewSection viewSection = this.m_view as ViewSection;
				if (viewSection.SketchPlane == null)
				{
					Plane plane2 = RevitVersionFuncs.CreatePlanByOriginAndBasis(viewSection.Origin, viewSection.RightDirection, viewSection.UpDirection);
					Transaction transaction2 = new Transaction(this.m_doc);
					transaction2.Start("new sketchPlane");
					SketchPlane sketchPlane2 = SketchPlane.Create(this.m_doc, plane2);
					this.m_view.SketchPlane = sketchPlane2;
					transaction2.Commit();
				}
				this.m_bViewPlan = false;
				return true;
			}
			return false;
		}

		public string GetUserTempDirectory()
		{
			string environmentVariable = Environment.GetEnvironmentVariable("TEMP");
			DirectoryInfo directoryInfo = new DirectoryInfo(environmentVariable);
			return directoryInfo.FullName + "\\";
		}

		public void DeleteFile(string filename)
		{
			if (File.Exists(filename))
			{
				File.Delete(filename);
			}
		}

		private Family LoadFamily(string familyFileName)
		{
			string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(familyFileName);
			Family family = null;
			Family result = null;
			Transaction transaction = new Transaction(this.m_doc, "LoadFamily");
			transaction.Start();
			try
			{
				if (!this.FindFamily(fileNameWithoutExtension, out family))
				{
					this.m_doc.LoadFamily(familyFileName, out family);
				}
				result = family;
				transaction.Commit();
			}
			catch (Exception)
			{
				transaction.RollBack();
			}
			if (family == null)
			{
				return null;
			}
			if (this.m_dlgMain != null)
			{
				Document document = this.m_doc.EditFamily(family);
				FilteredElementCollector filteredElementCollector = new FilteredElementCollector(document);
				filteredElementCollector.OfClass(typeof(TextElement));
                //this.GetUserTempDirectory() + fileNameWithoutExtension + ".rfa";
				Transaction transaction2 = new Transaction(document, "ModifyFamily");
				transaction2.Start();
				try
				{
					foreach (Element element in filteredElementCollector)
					{
						TextElement textElement = element as TextElement;
						if (textElement != null)
						{
							TextElementType textElementType = document.GetElement(textElement.GetTypeId()) as TextElementType;
							if (textElementType != null)
							{
								textElementType.GetParameter(BuiltInParameter.TEXT_SIZE).Set(Common.MMToFeet(this.m_dlgMain.DimensionStyleParam.m_dTextHeight));
								textElementType.GetParameter(BuiltInParameter.TEXT_WIDTH_SCALE).Set(this.m_dlgMain.DimensionStyleParam.m_dAspectRatio);
								Parameter parameter = textElement.GetParameter(BuiltInParameter.TEXT_ALIGN_HORZ);
								string text = textElement.Text;
								if (!(text == "标高说明\r") && !(text == "标高说明"))
								{
									if (this.m_dlgMain.DimStyleGraph == DimensionStyle.DS_FIVE || this.m_dlgMain.DimStyleGraph == DimensionStyle.DS_SIX)
									{
										parameter.Set(this.m_dlgMain.DimensionStyleParam.m_nTextAlign * 128);
									}
									else if (!this.m_bLeft)
									{
										parameter.Set(this.m_dlgMain.DimensionStyleParam.m_nTextAlign * 128);
									}
									else
									{
										int num = Math.Abs(this.m_dlgMain.DimensionStyleParam.m_nTextAlign - 2);
										parameter.Set(num * 128);
									}
								}
							}
						}
					}
					transaction2.Commit();
				}
				catch (Exception)
				{
					transaction2.RollBack();
					return null;
				}
				try
				{
					Family family2 = document.LoadFamily(this.m_doc, new FamilyLoadOpt());
					result = family2;
				}
				catch (Exception)
				{
					return null;
				}
				return result;
			}
			return result;
		}

		private Family LoadFamilyNoneTransaction(string familyFileName)
		{
			string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(familyFileName);
			Family family = null;
			Family result = null;
			try
			{
				if (!this.FindFamily(fileNameWithoutExtension, out family))
				{
					this.m_doc.LoadFamily(familyFileName, out family);
				}
				result = family;
			}
			catch (Exception)
			{
			}
			if (family == null)
			{
				return null;
			}
			return result;
		}

		private Family LoadSpotFamily(string familyFileName)
		{
			string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(familyFileName);
			Family family = this.FindFamily(fileNameWithoutExtension);
			if (family == null)
			{
				Family family2 = null;
				this.m_doc.LoadFamily(familyFileName, out family2);
				family = family2;
			}
			return family;
		}

		private Family FindFamily(string strFamilyName)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_doc);
			filteredElementCollector.OfClass(typeof(Family));
			if (filteredElementCollector != null)
			{
				IList<Element> list = filteredElementCollector.ToElements();
				foreach (Element element in list)
				{
					if (((Family)element).Name == strFamilyName)
					{
						return (Family)element;
					}
				}
			}
			return null;
		}

		private bool FindFamily(string strFamilyName, out Family family)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_doc);
			filteredElementCollector.OfClass(typeof(Family));
			if (filteredElementCollector != null)
			{
				IList<Element> list = filteredElementCollector.ToElements();
				foreach (Element element in list)
				{
					if (((Family)element).Name == strFamilyName)
					{
						family = (Family)element;
						return true;
					}
				}
			}
			family = null;
			return false;
		}

		private List<FamilySymbol> GetSymbols(Family family)
		{
			List<FamilySymbol> list = new List<FamilySymbol>();
			ISet<ElementId> familySymbolIds = family.GetFamilySymbolIds();
			foreach (ElementId elementId in familySymbolIds)
			{
				FamilySymbol item = family.Document.GetElement(elementId) as FamilySymbol;
				list.Add(item);
			}
			return list;
		}

		private FamilySymbol FamilySymbol(Family family, string strFamilySymbolName)
		{
			if (family == null || "" == strFamilySymbolName)
			{
				return null;
			}
			List<FamilySymbol> symbols = this.GetSymbols(family);
			if (0 >= symbols.Count)
			{
				return null;
			}
			FamilySymbol result = null;
			foreach (FamilySymbol familySymbol in symbols)
			{
				if (familySymbol.Name == strFamilySymbolName)
				{
					result = familySymbol;
					break;
				}
			}
			return result;
		}

		private void GetDrawOrientation(XYZ ptStart, XYZ ptEnd)
		{
			if (this.m_bViewPlan)
			{
				if (ptStart.X < ptEnd.X)
				{
					this.m_bLeft = false;
					return;
				}
				this.m_bLeft = true;
				return;
			}
			else
			{
				if (!this.m_view.ViewDirection.IsAlmostEqualTo(new XYZ(0.0, 1.0, 0.0)) && !this.m_view.ViewDirection.IsAlmostEqualTo(new XYZ(0.0, -1.0, 0.0)))
				{
					if (this.m_view.ViewDirection.IsAlmostEqualTo(new XYZ(1.0, 0.0, 0.0)) || this.m_view.ViewDirection.IsAlmostEqualTo(new XYZ(-1.0, 0.0, 0.0)))
					{
						if (ptStart.Y < ptEnd.Y)
						{
							this.m_bLeft = false;
							return;
						}
						this.m_bLeft = true;
					}
					return;
				}
				if (ptStart.X < ptEnd.X)
				{
					this.m_bLeft = false;
					return;
				}
				this.m_bLeft = true;
				return;
			}
		}

		private void CreateLevelDimension(XYZ ptInsert, Reference refEdge, Element elemSelected)
		{
			if (this.m_dlgMain.Automatic)
			{
				if (this.m_dlgMain.Associate)
				{
					this.CreateSpotDimensionFamily(refEdge);
					return;
				}
				this.CreateSymbolFamily(ptInsert, elemSelected, refEdge);
				return;
			}
			else
			{
				if (this.m_dlgMain.Manual && 1 == this.m_dlgMain.FloorInfos.Count)
				{
					this.CreateSymbolFamily(ptInsert, elemSelected);
					return;
				}
				List<Curve> dimenstionCurves = this.GetDimenstionCurves(ptInsert);
				this.CodeCreateDimensionGroup(ptInsert, dimenstionCurves);
				return;
			}
		}

		private void CreateSpotDimensionFamily(Reference refEdge)
		{
			XYZ globalPoint = refEdge.GlobalPoint;
			Transaction transaction = new Transaction(this.m_doc, "Level Dimension");
			try
			{
				transaction.Start();
				SpotDimension spotDimension = this.m_doc.Create.NewSpotElevation(this.m_view, refEdge, globalPoint, globalPoint, globalPoint, globalPoint, false);
				spotDimension.DimensionType = this.GetSpotDimensionType1();
				transaction.Commit();
			}
			catch (Exception)
			{
				if ((TransactionStatus)1 == transaction.GetStatus())
				{
					transaction.RollBack();
				}
			}
		}

		private void CreateSymbolFamily(XYZ ptDimInsert, Element element, Reference pickEdge)
		{
			Family family = this.LoadFamily(this.GetFamilyFileName());
			if (family == null)
			{
				YJKMessageBox.Show("没有找到加载的族", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				return;
			}
			Family family2 = null;
			string empty = string.Empty;
			string strNewFamilyName = string.Empty;
			DimensionStyleParameter dimensionStyleParam = this.m_dlgMain.DimensionStyleParam;
			string text = string.Empty;
			if (dimensionStyleParam.m_nTextAlign == 0)
			{
				text = " 左对齐";
			}
			else if (dimensionStyleParam.m_nTextAlign == 1)
			{
				text = " 中心线";
			}
			else
			{
				text = " 右对齐";
			}
			StringBuilder stringBuilder = new StringBuilder("HY-");
			stringBuilder.Append(dimensionStyleParam.m_strTextFont).Append("-").Append(dimensionStyleParam.m_dTextHeight.ToString());
			strNewFamilyName = string.Concat(new object[]
			{
				family.Name,
				" ",
				stringBuilder,
				text
			});
            if (!this.IsExistenceFamily(strNewFamilyName, ref family2))
			{
				this.ModifyFamily(family, ref empty);
				family2 = this.LoadFamily(empty);
			}
			FamilySymbol familySymbol = this.FamilySymbol(family2, "标准");
			if (familySymbol == null)
			{
				return;
			}
			FamilyInstance familyInstance = null;
			Transaction transaction = new Transaction(this.m_doc, "Create FamilyInstance");
			try
			{
				transaction.Start();
				familyInstance = this.m_doc.Create.NewFamilyInstance(ptDimInsert, familySymbol, this.m_view);
				if (familyInstance != null)
				{
					if (!this.m_bViewPlan)
					{
						if (this.m_view.ViewDirection.IsAlmostEqualTo(new XYZ(0.0, 1.0, 0.0)))
						{
							Plane plane;
							if (2016 < this.m_nRevitNumber)
							{
								plane = RevitVersionFuncs.CreatePlanByNormalAndOrigin(this.m_view.RightDirection, ptDimInsert);
							}
							else
							{
								plane = RevitVersionFuncs.CreatePlanByNormalAndOrigin(this.m_view.UpDirection, ptDimInsert);
							}
							List<ElementId> list = new List<ElementId>();
							list.Add(familyInstance.Id);
							RevitVersionFuncs.MirrorElements(this.m_doc, list, plane, false);
						}
						else if (this.m_view.ViewDirection.IsAlmostEqualTo(new XYZ(-1.0, 0.0, 0.0)))
						{
							Plane plane = RevitVersionFuncs.CreatePlanByNormalAndOrigin(this.m_view.RightDirection, ptDimInsert);
							List<ElementId> list2 = new List<ElementId>();
							list2.Add(familyInstance.Id);
							RevitVersionFuncs.MirrorElements(this.m_doc, list2, plane, false);
						}
					}
					this.ModifySymbolFamilyParameter(ref familyInstance, element, pickEdge);
					if (File.Exists(empty))
					{
						File.Delete(empty);
					}
					transaction.Commit();
				}
			}
			catch (Exception)
			{
				if ((TransactionStatus)1 == transaction.GetStatus())
				{
					transaction.RollBack();
				}
			}
		}

		private void ModifyFamily(Family family, ref string strTempFamilyPath)
		{
			Document document = this.m_doc.EditFamily(family);
			Transaction transaction = new Transaction(document);
			try
			{
				transaction.Start("Edit Family");
				FilteredElementCollector filteredElementCollector = new FilteredElementCollector(document);
				ICollection<Element> collection = filteredElementCollector.OfClass(typeof(TextElement)).ToElements();
				string strTextFont = string.Empty;
				foreach (Element element in collection)
				{
					TextElement textElement = (TextElement)element;
					strTextFont = this.GetFamilyTextFontAndType(document, textElement);
				}
				transaction.Commit();
				this.SaveNewFamily(document, family.Name, strTextFont, ref strTempFamilyPath);
			}
			catch
			{
				transaction.RollBack();
			}
		}

		private bool IsExistenceFamily(string strNewFamilyName, ref Family newFamily)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_doc);
			ElementClassFilter elementClassFilter = new ElementClassFilter(typeof(Family));
			filteredElementCollector.WherePasses(elementClassFilter);
			List<Element> source = filteredElementCollector.WherePasses(elementClassFilter).WhereElementIsNotElementType().ToElements() as List<Element>;
			IEnumerable<Element> enumerable = from e in source
			where e.Name.Contains(strNewFamilyName)
			select e;
			foreach (Element element in enumerable)
			{
				newFamily = (element as Family);
			}
			return newFamily != null;
		}

		private void SaveNewFamily(Document TempFamilyDoc, string strTempFamilyName, string strTextFont, ref string strTempFamilyPath)
		{
			try
			{
				strTempFamilyPath = Environment.GetEnvironmentVariable("TEMP");
				strTempFamilyPath = string.Concat(new string[]
				{
					strTempFamilyPath,
					"\\",
					strTempFamilyName,
					" ",
					strTextFont,
					".rfa"
				});
				if (File.Exists(strTempFamilyPath))
				{
					File.Delete(strTempFamilyPath);
				}
				SaveAsOptions saveAsOptions = new SaveAsOptions();
				saveAsOptions.OverwriteExistingFile = true;
				TempFamilyDoc.SaveAs(strTempFamilyPath, saveAsOptions);
				TempFamilyDoc.Close(false);
			}
			catch (Exception)
			{
				strTempFamilyPath = "";
			}
		}

		private string GetFamilyTextFontAndType(Document familyDoc, TextElement textElement)
		{
			DimensionStyleParameter dimensionStyleParam = this.m_dlgMain.DimensionStyleParam;
			string value = string.Empty;
			if (dimensionStyleParam.m_nTextAlign == 0)
			{
				value = " 左对齐";
			}
			else if (dimensionStyleParam.m_nTextAlign == 1)
			{
				value = " 中心线";
			}
			else
			{
				value = " 右对齐";
			}
			StringBuilder stringBuilder = new StringBuilder("HY-");
			stringBuilder.Append(dimensionStyleParam.m_strTextFont).Append("-").Append(dimensionStyleParam.m_dTextHeight.ToString()).Append("-").Append(dimensionStyleParam.m_dAspectRatio.ToString()).Append(value);
			TextElementType symbol = textElement.Symbol;
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(familyDoc);
			filteredElementCollector.OfClass(typeof(TextElementType));
            Autodesk.Revit.DB.ElementType elemType;
			foreach (Element element in filteredElementCollector)
			{
				if (string.Equals(element.Name, stringBuilder.ToString()))
				{
                    elemType = (element as Autodesk.Revit.DB.ElementType);
					this.SetFamilyTextFontAndType(elemType, textElement, stringBuilder);
					return stringBuilder.ToString();
				}
			}
			elemType = symbol.Duplicate(stringBuilder.ToString());
			this.SetFamilyTextFontAndType(elemType, textElement, stringBuilder);
			return stringBuilder.ToString();
		}

        private void SetFamilyTextFontAndType(Autodesk.Revit.DB.ElementType elemType, TextElement textElement, StringBuilder strNewTypeName)
		{
			TextElementType textElementType = elemType as TextElementType;
			ElementId id = textElementType.Id;
			textElement.ChangeTypeId(id);
			int num = strNewTypeName.ToString().IndexOf("-", 3);
			string text = strNewTypeName.ToString().Substring(3, num - 3);
			elemType.GetParameter("文字字体").Set(text);
		}

		private void CreateSymbolFamily(XYZ ptDimInsert, Element elemSelected)
		{
			Family family = this.LoadFamily(this.GetFamilyFileName());
			if (family == null)
			{
				YJKMessageBox.Show("没有找到加载的族", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				return;
			}
			Family family2 = null;
			string empty = string.Empty;
			string strNewFamilyName = string.Empty;
			DimensionStyleParameter dimensionStyleParam = this.m_dlgMain.DimensionStyleParam;
			string text = string.Empty;
			if (dimensionStyleParam.m_nTextAlign == 0)
			{
				text = " 左对齐";
			}
			else if (dimensionStyleParam.m_nTextAlign == 1)
			{
				text = " 中心线";
			}
			else
			{
				text = " 右对齐";
			}
			StringBuilder stringBuilder = new StringBuilder("HY-");
			stringBuilder.Append(dimensionStyleParam.m_strTextFont).Append("-").Append(dimensionStyleParam.m_dTextHeight.ToString());
			strNewFamilyName = string.Concat(new object[]
			{
				family.Name,
				" ",
				stringBuilder,
				text
			});
			if (!this.IsExistenceFamily(strNewFamilyName, ref family2))
			{
				this.ModifyFamily(family, ref empty);
				family2 = this.LoadFamily(empty);
			}
			FamilySymbol familySymbol = this.FamilySymbol(family2, "标准");
			if (familySymbol == null)
			{
				return;
			}
			FamilyInstance familyInstance = null;
			Transaction transaction = new Transaction(this.m_doc, "Create FamilyInstance");
			try
			{
				transaction.Start();
				familyInstance = this.m_doc.Create.NewFamilyInstance(ptDimInsert, familySymbol, this.m_view);
				if (familyInstance != null)
				{
					if (!this.m_bViewPlan)
					{
						if (this.m_view.ViewDirection.IsAlmostEqualTo(new XYZ(0.0, 1.0, 0.0)))
						{
							Plane plane;
							if (2016 < this.m_nRevitNumber)
							{
								plane = RevitVersionFuncs.CreatePlanByNormalAndOrigin(this.m_view.RightDirection, ptDimInsert);
							}
							else
							{
								plane = RevitVersionFuncs.CreatePlanByNormalAndOrigin(this.m_view.UpDirection, ptDimInsert);
							}
							List<ElementId> list = new List<ElementId>();
							list.Add(familyInstance.Id);
							RevitVersionFuncs.MirrorElements(this.m_doc, list, plane, false);
						}
						else if (this.m_view.ViewDirection.IsAlmostEqualTo(new XYZ(-1.0, 0.0, 0.0)))
						{
							Plane plane = RevitVersionFuncs.CreatePlanByNormalAndOrigin(this.m_view.RightDirection, ptDimInsert);
							List<ElementId> list2 = new List<ElementId>();
							list2.Add(familyInstance.Id);
							RevitVersionFuncs.MirrorElements(this.m_doc, list2, plane, false);
						}
					}
					this.ModifySymbolFamilyParameter(ref familyInstance, elemSelected);
					transaction.Commit();
				}
			}
			catch (Exception)
			{
				if ((TransactionStatus)1 == transaction.GetStatus())
				{
					transaction.RollBack();
				}
			}
		}

		private ElementId GetFillRegionTypeId()
		{
			bool flag = false;
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_doc);
			filteredElementCollector.OfClass(typeof(FilledRegionType));
			FilledRegionType filledRegionType = null;
			foreach (Element element in filteredElementCollector)
			{
				filledRegionType = (element as FilledRegionType);
				if (filledRegionType != null && "实体填充 - 黑色" == filledRegionType.Name)
				{
					return filledRegionType.Id;
				}
			}
			if (!flag)
			{
				return this.LoadFilledRegionType(filledRegionType).Id;
			}
			filledRegionType = (filteredElementCollector.First<Element>() as FilledRegionType);
			if (filledRegionType == null)
			{
				return null;
			}
			return filledRegionType.Id;
		}

		private FilledRegionType LoadFilledRegionType(FilledRegionType resRegionType)
		{
			FilledRegionType filledRegionType = resRegionType.Duplicate("实体填充 - 黑色") as FilledRegionType;
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_doc);
			filteredElementCollector.OfClass(typeof(FillPatternElement));
			foreach (Element element in filteredElementCollector)
			{
				FillPatternElement fillPatternElement = element as FillPatternElement;
				if (fillPatternElement.Name == "实体填充")
				{
					ElementId id = fillPatternElement.Id;
					filledRegionType.get_Parameter(BuiltInParameter.ANY_PATTERN_ID_PARAM_NO_NO).Set(id);
					break;
				}
			}
			return filledRegionType;
		}

		private void CodeCreateDimensionGroup(XYZ ptBase, List<Curve> lstCurves)
		{
			ElementSet elementSet = new ElementSet();
			Transaction transaction = new Transaction(this.m_doc);
			IList<CurveLoop> list = new List<CurveLoop>();
			CurveLoop curveLoop = new CurveLoop();
			FilledRegion filledRegion = null;
			try
			{
				transaction.Start("Create DetailCurve");
				switch (this.m_dlgMain.DimStyleGraph)
				{
				case DimensionStyle.DS_ONE:
				case DimensionStyle.DS_TWO:
				case DimensionStyle.DS_THREE:
				case DimensionStyle.DS_FOUR:
					using (List<Curve>.Enumerator enumerator = lstCurves.GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							Curve curve = enumerator.Current;
							DetailCurve detailCurve = this.m_doc.Create.NewDetailCurve(this.m_doc.ActiveView, curve);
							elementSet.Insert(detailCurve);
						}
						goto IL_10C;
					}
					break;
				case DimensionStyle.DS_FIVE:
				case DimensionStyle.DS_SIX:
					break;
				default:
					goto IL_10C;
				}
				foreach (Curve curve2 in lstCurves)
				{
					curveLoop.Append(curve2);
				}
				list.Add(curveLoop);
				filledRegion = FilledRegion.Create(this.m_doc, this.GetFillRegionTypeId(), this.m_view.Id, list);
				IL_10C:
				transaction.Commit();
				if (filledRegion != null)
				{
					elementSet.Insert(filledRegion);
				}
				this.CreateTextNotes(ptBase, ref elementSet);
				transaction.Start("Set the font ");
				foreach (object obj in elementSet)
				{
					if (obj is TextNote)
					{
						TextNote textNote = obj as TextNote;
						this.GetTextFontAndType(textNote);
					}
				}
				transaction.Commit();
				transaction.Start("Craete Group");
				Group group = RevitVersionFuncs.NewGroup(this.m_doc, elementSet);
				int num = 1;
				string text = string.Empty;
				text = "标高标注" + num.ToString();
				FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_doc);
				filteredElementCollector.OfClass(typeof(Group));
				foreach (Element element in filteredElementCollector)
				{
					Group group2 = element as Group;
					if (string.Equals(group2.GroupType.Name, text))
					{
						num++;
						text = "标高标注" + num.ToString();
					}
				}
				group.GroupType.Name = text;
				if (!this.m_bViewPlan && (this.m_view.ViewDirection.IsAlmostEqualTo(new XYZ(0.0, 1.0, 0.0)) || this.m_view.ViewDirection.IsAlmostEqualTo(new XYZ(-1.0, 0.0, 0.0))))
				{
					Plane plane = RevitVersionFuncs.CreatePlanByNormalAndOrigin(this.m_view.RightDirection, ptBase);
					List<ElementId> list2 = new List<ElementId>();
					list2.Add(group.Id);
					RevitVersionFuncs.MirrorElements(this.m_doc, list2, plane, false);
				}
				transaction.Commit();
			}
			catch (Exception)
			{
				transaction.RollBack();
			}
		}

		private void GetTextFontAndType(TextNote textNote)
		{
			DimensionStyleParameter dimensionStyleParam = this.m_dlgMain.DimensionStyleParam;
			StringBuilder stringBuilder = new StringBuilder("HY-");
			stringBuilder.Append(dimensionStyleParam.m_strTextFont).Append("-").Append(dimensionStyleParam.m_dTextHeight.ToString()).Append("-").Append(dimensionStyleParam.m_dAspectRatio.ToString());
			TextElementType symbol = textNote.Symbol;
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_doc);
			filteredElementCollector.OfClass(typeof(TextElementType));
            Autodesk.Revit.DB.ElementType elemType;
			foreach (Element element in filteredElementCollector)
			{
				if (string.Equals(element.Name, stringBuilder.ToString()))
				{
                    elemType = (element as Autodesk.Revit.DB.ElementType);
					this.SetTextFontAndType(elemType, textNote, stringBuilder);
					return;
				}
			}
			elemType = symbol.Duplicate(stringBuilder.ToString());
			this.SetTextFontAndType(elemType, textNote, stringBuilder);
		}

        private void SetTextFontAndType(Autodesk.Revit.DB.ElementType elemType, TextNote textNote, StringBuilder strNewTypeName)
		{
			TextElementType textElementType = elemType as TextElementType;
			ElementId id = textElementType.Id;
			textNote.ChangeTypeId(id);
			int num = strNewTypeName.ToString().IndexOf("-", 3);
			string text = strNewTypeName.ToString().Substring(3, num - 3);
			elemType.GetParameter("文字字体").Set(text);
		}

		private SpotDimensionType GetSpotDimensionType()
		{
			string familyFileName = this.GetFamilyFileName();
			if (this.LoadSpotFamily(familyFileName) == null)
			{
				YJKMessageBox.Show("没有找到加载的族", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				return null;
			}
			string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(familyFileName);
			SpotDimensionType spotType = this.GetSpotType();
			if (spotType == null)
			{
				return null;
			}
			string newTypeName = this.GetNewTypeName(fileNameWithoutExtension);
			SpotDimensionType spotDimensionType = spotType.Duplicate(newTypeName) as SpotDimensionType;
			if (spotDimensionType == null)
			{
				return null;
			}
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_doc);
			IList<Element> list = filteredElementCollector.OfCategory(BuiltInCategory.OST_SpotElevSymbols).ToElements();
			if (0 >= list.Count)
			{
				return null;
			}
			FamilySymbol familySymbol = null;
			foreach (Element element in list)
			{
				if (fileNameWithoutExtension == (element as FamilySymbol).Family.Name)
				{
					familySymbol = (element as FamilySymbol);
					break;
				}
			}
			if (familySymbol == null)
			{
				return null;
			}
			DimensionStyleParameter dimensionStyleParam = this.m_dlgMain.DimensionStyleParam;
			spotDimensionType.GetParameter("符号").Set(familySymbol.Id);
			spotDimensionType.GetParameter("文字与符号的偏移量").Set(0.0);
			spotDimensionType.GetParameter("文字距引线的偏移量").Set(Common.MMToFeet(6.0));
			spotDimensionType.GetParameter("文字方向").Set(0);
			spotDimensionType.GetParameter("文字位置").Set(0);
			spotDimensionType.GetParameter("文字字体").Set("仿宋");
			spotDimensionType.GetParameter("文字大小").Set(Common.MMToFeet(dimensionStyleParam.m_dTextHeight));
			spotDimensionType.GetParameter("宽度系数").Set(dimensionStyleParam.m_dAspectRatio);
			return spotDimensionType;
		}

		private SpotDimensionType GetSpotDimensionType1()
		{
			SpotDimensionType spotDimensionType = null;
			string spotDimensionTypeRvtFileName = this.GetSpotDimensionTypeRvtFileName();
			string spotDimensionTypeName = this.GetSpotDimensionTypeName();
			Document document = this.m_cmdData.Application.Application.OpenDocumentFile(spotDimensionTypeRvtFileName);
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(document);
			IList<Element> list = filteredElementCollector.OfClass(typeof(SpotDimensionType)).ToElements();
			foreach (Element element in list)
			{
				SpotDimensionType spotDimensionType2 = element as SpotDimensionType;
				if (spotDimensionType2 != null && spotDimensionType2.Name == spotDimensionTypeName)
				{
					spotDimensionType = spotDimensionType2;
					break;
				}
			}
			CopyPasteOptions copyPasteOptions = new CopyPasteOptions();
			ICollection<ElementId> source = ElementTransformUtils.CopyElements(document, new List<ElementId>
			{
				spotDimensionType.Id
			}, this.m_doc, null, copyPasteOptions);
			spotDimensionType = (this.m_doc.GetElement(source.First<ElementId>()) as SpotDimensionType);
			DimensionStyleParameter dimensionStyleParam = this.m_dlgMain.DimensionStyleParam;
			spotDimensionType.GetParameter("文字与符号的偏移量").Set(0.0);
			spotDimensionType.GetParameter("文字方向").Set(0);
			spotDimensionType.GetParameter("文字字体").Set(dimensionStyleParam.m_strTextFont);
			spotDimensionType.GetParameter("文字大小").Set(Common.MMToFeet(dimensionStyleParam.m_dTextHeight));
			spotDimensionType.GetParameter("宽度系数").Set(dimensionStyleParam.m_dAspectRatio);
			return spotDimensionType;
		}

		private string GetSpotDimensionTypeRvtFileName()
		{
			string versionNumber = this.m_doc.Application.VersionNumber;
			return Product.InstallLocation + "\\Templates\\" + versionNumber + "\\ScrTemplate.rvt";
		}

		private string GetSpotDimensionTypeName()
		{
			string result = "";
			switch (this.m_dlgMain.DimensionStyleGraph)
			{
			case DimensionStyle.DS_ONE:
				result = "高程点_普通标高1";
				break;
			case DimensionStyle.DS_TWO:
				result = "高程点_普通标高-带基线1";
				break;
			case DimensionStyle.DS_THREE:
				result = "高程点_普通标高-带引线1";
				break;
			case DimensionStyle.DS_FOUR:
				result = "高程点_普通标高-带基线-带引线1";
				break;
			case DimensionStyle.DS_FIVE:
				result = "高程点_三角形室外标高1";
				break;
			case DimensionStyle.DS_SIX:
				result = "高程点_圆点室外标高1";
				break;
			}
			return result;
		}

		private SpotDimensionType GetSpotType()
		{
			SpotDimensionType result = null;
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_doc);
			IList<Element> list = filteredElementCollector.OfClass(typeof(SpotDimensionType)).ToElements();
			foreach (Element element in list)
			{
				Parameter parameter = element.get_Parameter(BuiltInParameter.ALL_MODEL_FAMILY_NAME);
				if (parameter.AsString() == "高程点")
				{
					result = (element as SpotDimensionType);
					break;
				}
			}
			return result;
		}

		private string GetNewTypeName(string strFamilyName)
		{
			int num = 1;
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_doc);
			IList<Element> list = filteredElementCollector.OfClass(typeof(SpotDimensionType)).ToElements();
			foreach (Element element in list)
			{
				if (element.Name == strFamilyName + num.ToString())
				{
					num++;
				}
			}
			return strFamilyName + num.ToString();
		}

		private void ModifySymbolFamilyParameter(ref FamilyInstance familyInstance, Element elem, Reference pickEdge)
		{
			string text = "";
			string text2;
			if (pickEdge == null)
			{
				List<string> source = this.m_dlgMain.FloorInfos.Values.ElementAt(0);
				text2 = source.ElementAt(0);
				text2 = LevelDimensionManager.FloorLevelByPrecision(text2, this.m_dlgMain.DimensionStyleParam.m_nPrecision);
				text2 = LevelDimensionManager.FloorLevelAddPrefix(text2);
				text = source.ElementAt(1);
				text = LevelDimensionManager.FloorInforAddParenthesis(text, this.m_dlgMain.AddParenthesis);
			}
			else
			{
				Level level = this.m_doc.GetElement(elem.LevelId) as Level;
				string strText = (Common.FeetToMM(pickEdge.GlobalPoint.Z) / 1000.0).ToString();
				text2 = LevelDimensionManager.FloorLevelByPrecision(strText, this.m_dlgMain.DimensionStyleParam.m_nPrecision);
				text2 = LevelDimensionManager.FloorLevelAddPrefix(text2);
				if (this.m_dlgMain.AutoAddFloorName)
				{
					text = level.Name;
					text = LevelDimensionManager.FloorInforAddParenthesis(level.Name, this.m_dlgMain.AddParenthesis);
				}
			}
			familyInstance.GetParameter("标高1").Set(text2);
			if ("" != text)
			{
				familyInstance.GetParameter("标高说明").Set(text);
				familyInstance.GetParameter("标高说明可见性").Set(1);
				return;
			}
			familyInstance.GetParameter("标高说明").Set("");
			familyInstance.GetParameter("标高说明可见性").Set(0);
		}

		private void ModifySymbolFamilyParameter(ref FamilyInstance familyInstance, Element elemSelected)
		{
			List<string> source = this.m_dlgMain.FloorInfos.Values.ElementAt(0);
			string text = source.ElementAt(0);
			text = LevelDimensionManager.FloorLevelByPrecision(text, this.m_dlgMain.DimensionStyleParam.m_nPrecision);
			text = LevelDimensionManager.FloorLevelAddPrefix(text);
			string text2 = source.ElementAt(1);
			text2 = LevelDimensionManager.FloorInforAddParenthesis(text2, this.m_dlgMain.AddParenthesis);
			familyInstance.GetParameter("标高1").Set(text);
			if ("" != text2)
			{
				familyInstance.GetParameter("标高说明").Set(text2);
				familyInstance.GetParameter("标高说明可见性").Set(1);
				return;
			}
			familyInstance.GetParameter("标高说明").Set("");
			familyInstance.GetParameter("标高说明可见性").Set(0);
		}

		private void ModifySymbolFamiylParameterNoneForm(ref FamilyInstance familyInstance, Element elemSelected = null, double inputValue = 0.0, int usePrecision = 0)
		{
			string text = "";
			string strText;
			if (elemSelected == null)
			{
				strText = (Common.FeetToMM(inputValue) / 1000.0).ToString();
			}
			else
			{
				Level level = this.m_doc.GetElement(elemSelected.LevelId) as Level;
				strText = (Common.FeetToMM(level.Elevation) / 1000.0).ToString();
			}
			string text2 = LevelDimensionManager.FloorLevelByPrecision(strText, usePrecision);
			text2 = LevelDimensionManager.FloorLevelAddPrefix(text2);
			familyInstance.GetParameter("标高1").Set(text2);
			if ("" != text)
			{
				familyInstance.GetParameter("标高说明").Set(text);
				familyInstance.GetParameter("标高说明可见性").Set(1);
				return;
			}
			familyInstance.GetParameter("标高说明").Set("");
			familyInstance.GetParameter("标高说明可见性").Set(0);
		}

		private void CreateTextNotes(XYZ ptBase, ref ElementSet elemSet)
		{
			Transaction transaction = new Transaction(this.m_doc);
			try
			{
				transaction.Start("Create Text");
				DimensionStyleParameter dimensionStyleParam = this.m_dlgMain.DimensionStyleParam;
				SpecialCharactorDrawingKit specialCharactorDrawingKit = new SpecialCharactorDrawingKit(this.m_cmdData, dimensionStyleParam.m_strTextFont, Common.MMToFeet(dimensionStyleParam.m_dTextHeight), dimensionStyleParam.m_dAspectRatio);
				SpecialCharactorDrawingKit.TextHorizontalAlignment hAlignment = SpecialCharactorDrawingKit.TextHorizontalAlignment.THA_LEFT;
				XYZ xyz = Common.MMToFeet((this.m_dlgMain.DimensionStyleParam.m_dTextHeight + 1.5) * (double)this.m_view.Scale) * this.m_view.UpDirection;
				if (1 == this.m_dlgMain.HeaderStyle)
				{
					xyz = -xyz;
				}
				XYZ ptBase2 = null;
				XYZ xyz2 = null;
				XYZ xyz3 = null;
				foreach (List<string> list in this.m_dlgMain.FloorInfos.Values)
				{
					for (int i = 0; i < list.Count; i++)
					{
						string text = list.ElementAt(i);
						this.SetDimensionText(text);
						if (i == 0)
						{
							text = LevelDimensionManager.FloorLevelByPrecision(text, this.m_dlgMain.DimensionStyleParam.m_nPrecision);
							text = LevelDimensionManager.FloorLevelAddPrefix(text);
						}
						if (i == 0 && (1 == dimensionStyleParam.m_nTextAlign || DimensionStyle.DS_FIVE == this.m_dlgMain.DimensionStyleGraph || DimensionStyle.DS_SIX == this.m_dlgMain.DimensionStyleGraph))
						{
							this.CalculateTextCenterAlignInsert(ptBase, text, ref xyz2, ref xyz3);
						}
						else if (i == 0)
						{
							this.CalculateTextRLAlignInsert(ptBase, ref xyz2, ref xyz3);
						}
						if (i == 0)
						{
							ptBase2 = xyz2;
							hAlignment = this.GetTextHorizontalAlign();
						}
						else if (1 == i)
						{
							ptBase2 = xyz3;
							if (this.m_bLeft)
							{
								hAlignment = SpecialCharactorDrawingKit.TextHorizontalAlignment.THA_RIGHT;
							}
							else
							{
								hAlignment = SpecialCharactorDrawingKit.TextHorizontalAlignment.THA_LEFT;
							}
							text = LevelDimensionManager.FloorInforAddParenthesis(text, this.m_dlgMain.AddParenthesis);
						}
						List<Element> list2 = specialCharactorDrawingKit.DrawSingleRowCharactors(ptBase2, text, hAlignment, SpecialCharactorDrawingKit.TextVerticalAlignment.TVA_MIDDLE);
						foreach (Element element in list2)
						{
							elemSet.Insert(element);
						}
					}
					xyz2 += xyz;
					xyz3 += xyz;
					ptBase += xyz;
				}
				transaction.Commit();
			}
			catch (Exception)
			{
				transaction.RollBack();
			}
		}

		private void CalculateTextRLAlignInsert(XYZ ptBase, ref XYZ ptFloorLevel, ref XYZ ptFloorName)
		{
			XYZ xyz = null;
			XYZ xyz2 = null;
			this.GetOffsetValue(ref ptBase, ref xyz, ref xyz2);
			double floorLevelLength = this.GetFloorLevelLength(this.m_strDimensionText.Replace("±", ""));
			DimensionStyleParameter dimensionStyleParam = this.m_dlgMain.DimensionStyleParam;
			if (dimensionStyleParam.m_nTextAlign != 0)
			{
				if (2 == dimensionStyleParam.m_nTextAlign)
				{
					switch (this.m_dlgMain.DimensionStyleGraph)
					{
					case DimensionStyle.DS_ONE:
					case DimensionStyle.DS_TWO:
						ptFloorLevel = ptBase + 4.0 * xyz + xyz2;
						break;
					case DimensionStyle.DS_THREE:
					case DimensionStyle.DS_FOUR:
						ptFloorLevel = ptBase + 4.0 * xyz + 2.0 * xyz2;
						break;
					}
					ptFloorName = ptFloorLevel;
				}
				return;
			}
			switch (this.m_dlgMain.DimensionStyleGraph)
			{
			case DimensionStyle.DS_ONE:
			case DimensionStyle.DS_TWO:
				ptFloorLevel = ptBase - xyz + xyz2;
				if (0.0 < 1500.0 - floorLevelLength)
				{
					ptFloorName = ptFloorLevel + 5.0 * xyz;
					return;
				}
				if (!this.m_bLeft)
				{
					ptFloorName = ptFloorLevel + Common.MMToFeet(floorLevelLength) * this.m_view.RightDirection;
					return;
				}
				ptFloorName = ptFloorLevel - Common.MMToFeet(floorLevelLength) * this.m_view.RightDirection;
				return;
			case DimensionStyle.DS_THREE:
			case DimensionStyle.DS_FOUR:
				ptFloorLevel = ptBase + 2.0 * xyz2;
				if (0.0 < 1200.0 - floorLevelLength)
				{
					ptFloorName = ptFloorLevel + 4.0 * xyz;
					return;
				}
				if (!this.m_bLeft)
				{
					ptFloorName = ptFloorLevel + Common.MMToFeet(floorLevelLength) * this.m_view.RightDirection;
					return;
				}
				ptFloorName = ptFloorLevel - Common.MMToFeet(floorLevelLength) * this.m_view.RightDirection;
				return;
			default:
				return;
			}
		}

		private void CalculateTextCenterAlignInsert(XYZ ptBase, string strText, ref XYZ ptFloorLevel, ref XYZ ptFloorName)
		{
			XYZ xyz = null;
			XYZ xyz2 = null;
			this.GetOffsetValue(ref ptBase, ref xyz, ref xyz2);
			double floorLevelLength = this.GetFloorLevelLength(strText.Replace("±", ""));
			if (1 == this.m_dlgMain.DimensionStyleParam.m_nTextAlign || DimensionStyle.DS_FIVE == this.m_dlgMain.DimensionStyleGraph || DimensionStyle.DS_SIX == this.m_dlgMain.DimensionStyleGraph)
			{
				switch (this.m_dlgMain.DimensionStyleGraph)
				{
				case DimensionStyle.DS_ONE:
				case DimensionStyle.DS_TWO:
				{
					XYZ xyz3 = Common.MMToFeet(1500.0 - floorLevelLength) / 2.0 * this.m_view.RightDirection;
					if (this.m_bLeft)
					{
						xyz3 = -xyz3;
					}
					XYZ xyz4 = ptBase - xyz + xyz2;
					ptFloorLevel = xyz4 + xyz3;
					if (floorLevelLength < 1500.0 - (1500.0 - floorLevelLength) / 2.0)
					{
						ptFloorName = xyz4 + 5.0 * xyz;
						return;
					}
					if (this.m_bLeft)
					{
						ptFloorName = ptFloorLevel - Common.MMToFeet(floorLevelLength) * this.m_view.RightDirection;
						return;
					}
					ptFloorName = ptFloorLevel + Common.MMToFeet(floorLevelLength) * this.m_view.RightDirection;
					return;
				}
				case DimensionStyle.DS_THREE:
				case DimensionStyle.DS_FOUR:
				{
					XYZ xyz3 = Common.MMToFeet(1200.0 - floorLevelLength) / 2.0 * this.m_view.RightDirection;
					if (this.m_bLeft)
					{
						xyz3 = -xyz3;
					}
					XYZ xyz4 = ptBase + 2.0 * xyz2;
					ptFloorLevel = xyz4 + xyz3;
					if (floorLevelLength < 1200.0 - (1200.0 - floorLevelLength) / 2.0)
					{
						ptFloorName = xyz4 + 4.0 * xyz;
						return;
					}
					if (this.m_bLeft)
					{
						ptFloorName = ptFloorLevel - Common.MMToFeet(floorLevelLength) * this.m_view.RightDirection;
						return;
					}
					ptFloorName = ptFloorLevel + Common.MMToFeet(floorLevelLength) * this.m_view.RightDirection;
					return;
				}
				case DimensionStyle.DS_FIVE:
				{
					XYZ xyz3 = Common.MMToFeet(600.0 - floorLevelLength) / 2.0 * this.m_view.RightDirection;
					if (this.m_bLeft)
					{
						xyz3 = -xyz3;
					}
					XYZ xyz4 = ptBase - xyz + xyz2;
					ptFloorLevel = xyz4 + xyz3;
					if (floorLevelLength < 600.0 - (600.0 - floorLevelLength) / 2.0)
					{
						ptFloorName = xyz4 + xyz;
						return;
					}
					if (this.m_bLeft)
					{
						ptFloorName = ptFloorLevel - Common.MMToFeet(floorLevelLength) * this.m_view.RightDirection;
						return;
					}
					ptFloorName = ptFloorLevel + Common.MMToFeet(floorLevelLength) * this.m_view.RightDirection;
					return;
				}
				case DimensionStyle.DS_SIX:
				{
					XYZ xyz3 = Common.MMToFeet(200.0 - floorLevelLength) / 2.0 * this.m_view.RightDirection;
					if (this.m_bLeft)
					{
						xyz3 = -xyz3;
					}
					XYZ xyz4 = ptBase - xyz / 3.0 + xyz2 / 3.0;
					ptFloorLevel = xyz4 + xyz3;
					if (floorLevelLength < 200.0 - (200.0 - floorLevelLength) / 2.0)
					{
						if (this.m_bLeft)
						{
							ptFloorName = xyz4 - xyz / 3.0 * 2.0;
							return;
						}
						ptFloorName = xyz4 + xyz / 3.0 * 2.0;
						return;
					}
					else
					{
						if (this.m_bLeft)
						{
							ptFloorName = ptFloorLevel - Common.MMToFeet(floorLevelLength) * this.m_view.RightDirection;
							return;
						}
						ptFloorName = ptFloorLevel + Common.MMToFeet(floorLevelLength) * this.m_view.RightDirection;
					}
					break;
				}
				default:
					return;
				}
			}
		}

		private void GetOffsetValue(ref XYZ ptBase, ref XYZ vecHOffset, ref XYZ vecVOffset)
		{
			double num = Common.MMToFeet(300.0);
			XYZ xyz = Common.MMToFeet(this.m_dlgMain.DimensionStyleParam.m_dTextHeight * (double)this.m_view.Scale) * this.m_view.UpDirection;
			vecHOffset = num * this.m_view.RightDirection;
			vecVOffset = num * this.m_view.UpDirection;
			if (this.m_bLeft)
			{
				vecHOffset = -vecHOffset;
			}
			if (1 == this.m_dlgMain.HeaderStyle)
			{
				vecVOffset = -vecVOffset;
				ptBase -= xyz;
				return;
			}
			ptBase += xyz;
		}

		private double GetFloorLevelLength(string strFloorLevelLength)
		{
			if (strFloorLevelLength == null)
			{
				return 0.0;
			}
			if (strFloorLevelLength.Length == 0)
			{
				return 0.0;
			}
			ASCIIEncoding asciiencoding = new ASCIIEncoding();
			int num = 0;
			byte[] bytes = asciiencoding.GetBytes(strFloorLevelLength);
			for (int i = 0; i < bytes.Length; i++)
			{
				if (bytes[i] == 63)
				{
					num += 2;
				}
				else
				{
					num++;
				}
			}
			DimensionStyleParameter dimensionStyleParam = this.m_dlgMain.DimensionStyleParam;
			double num2 = dimensionStyleParam.m_dTextHeight * (double)this.m_doc.ActiveView.Scale;
			double dAspectRatio = dimensionStyleParam.m_dAspectRatio;
			double num3 = num2 * dAspectRatio;
			return (double)num * num3;
		}

		private SpecialCharactorDrawingKit.TextHorizontalAlignment GetTextHorizontalAlign()
		{
			SpecialCharactorDrawingKit.TextHorizontalAlignment result = SpecialCharactorDrawingKit.TextHorizontalAlignment.THA_LEFT;
			if (DimensionStyle.DS_FIVE == this.m_dlgMain.DimensionStyleGraph || DimensionStyle.DS_SIX == this.m_dlgMain.DimensionStyleGraph)
			{
				if (this.m_bLeft)
				{
					result = SpecialCharactorDrawingKit.TextHorizontalAlignment.THA_RIGHT;
				}
				return result;
			}
			DimensionStyleParameter dimensionStyleParam = this.m_dlgMain.DimensionStyleParam;
			if (dimensionStyleParam.m_nTextAlign == 0 || 1 == dimensionStyleParam.m_nTextAlign)
			{
				if (this.m_bLeft)
				{
					result = SpecialCharactorDrawingKit.TextHorizontalAlignment.THA_RIGHT;
				}
				else
				{
					result = SpecialCharactorDrawingKit.TextHorizontalAlignment.THA_LEFT;
				}
			}
			else if (2 == dimensionStyleParam.m_nTextAlign)
			{
				if (this.m_bLeft)
				{
					result = SpecialCharactorDrawingKit.TextHorizontalAlignment.THA_LEFT;
				}
				else
				{
					result = SpecialCharactorDrawingKit.TextHorizontalAlignment.THA_RIGHT;
				}
			}
			return result;
		}

		private void SetDimensionText(string strText)
		{
			this.m_strDimensionText = strText;
		}

		private List<Curve> GetDimenstionCurves(XYZ ptDimensionInsert)
		{
			List<Curve> result = new List<Curve>();
			switch (this.m_dlgMain.DimensionStyleGraph)
			{
			case DimensionStyle.DS_ONE:
				LevelDimensionManager.DimensionStyleGraphOne(ref result, this.m_view, ptDimensionInsert, 300.0, this.m_dlgMain.HeaderStyle, this.m_bLeft);
				break;
			case DimensionStyle.DS_TWO:
				LevelDimensionManager.DimensionStyleGraphTwo(ref result, this.m_view, ptDimensionInsert, 300.0, this.m_dlgMain.HeaderStyle, this.m_bLeft);
				break;
			case DimensionStyle.DS_THREE:
				LevelDimensionManager.DimensionStyleGraphThree(ref result, this.m_view, ptDimensionInsert, 300.0, this.m_dlgMain.HeaderStyle, this.m_bLeft);
				break;
			case DimensionStyle.DS_FOUR:
				LevelDimensionManager.DimensionStyleGraphFour(ref result, this.m_view, ptDimensionInsert, 300.0, this.m_dlgMain.HeaderStyle, this.m_bLeft);
				break;
			case DimensionStyle.DS_FIVE:
				LevelDimensionManager.DimensionStyleGraphFive(ref result, this.m_view, ptDimensionInsert, 300.0, this.m_dlgMain.HeaderStyle, this.m_bLeft);
				break;
			case DimensionStyle.DS_SIX:
				LevelDimensionManager.DimensionStyleGraphSix(ref result, this.m_view, ptDimensionInsert, 100.0, this.m_dlgMain.HeaderStyle, this.m_bLeft);
				break;
			}
			return result;
		}

		private string GetFamilyFileName()
		{
			string result = "";
			string versionNumber = this.m_doc.Application.VersionNumber;
			if (this.m_dlgMain.Automatic)
			{
				if (this.m_dlgMain.Associate)
				{
					result = string.Concat(new string[]
					{
						Product.FamilyLocation,
						"\\",
						versionNumber,
						"\\LevelDimension\\SpotElevation\\",
						this.GetFamilyName()
					});
				}
				else
				{
					result = string.Concat(new string[]
					{
						Product.FamilyLocation,
						"\\",
						versionNumber,
						"\\LevelDimension\\LevelSymbol\\",
						this.GetFamilyName()
					});
				}
			}
			else if (this.m_dlgMain.Manual && 1 == this.m_dlgMain.FloorInfos.Count)
			{
				result = string.Concat(new string[]
				{
					Product.FamilyLocation,
					"\\",
					versionNumber,
					"\\LevelDimension\\LevelSymbol\\",
					this.GetFamilyName()
				});
			}
			return result;
		}

        public FamilyInstance CreateSymbolFamily(XYZ ptDimInsert, Autodesk.Revit.DB.View useView, Element elemSelected, double inputValue, bool ifHasMakeTrasaciton = false, string famiyName = "符号_普通标高(左上).rfa")
		{
			FamilyInstance familyInstance = null;
			Family family;
			if (ifHasMakeTrasaciton)
			{
				family = this.LoadFamilyNoneTransaction(this.GetFamilyFileName(famiyName));
			}
			else
			{
				family = this.LoadFamily(this.GetFamilyFileName(famiyName));
			}
			if (family == null)
			{
				return familyInstance;
			}
			FamilySymbol familySymbol = this.FamilySymbol(family, "标准");
			if (familySymbol == null)
			{
				return familyInstance;
			}
			if (!ifHasMakeTrasaciton)
			{
				Transaction transaction = new Transaction(this.m_doc, "Create FamilyInstance");
				try
				{
					transaction.Start();
					familyInstance = this.m_doc.Create.NewFamilyInstance(ptDimInsert, familySymbol, useView);
					if (familyInstance == null)
					{
						return familyInstance;
					}
					this.ModifySymbolFamiylParameterNoneForm(ref familyInstance, elemSelected, inputValue, 1);
					transaction.Commit();
					return familyInstance;
				}
				catch (Exception)
				{
					if ((TransactionStatus)1 == transaction.GetStatus())
					{
						transaction.RollBack();
					}
					return familyInstance;
				}
			}
			try
			{
				familyInstance = this.m_doc.Create.NewFamilyInstance(ptDimInsert, familySymbol, useView);
				this.ModifySymbolFamiylParameterNoneForm(ref familyInstance, elemSelected, inputValue, 1);
			}
			catch (Exception)
			{
			}
			return familyInstance;
		}

		public string GetFamilyFileName(string fileName)
		{
			string versionNumber = this.m_doc.Application.VersionNumber;
			return string.Concat(new string[]
			{
				Product.FamilyLocation,
				"\\",
				versionNumber,
				"\\LevelDimension\\LevelSymbol\\",
				fileName
			});
		}

		private string GetFamilyName()
		{
			string result = "";
			if (this.m_dlgMain.Automatic && this.m_dlgMain.Associate)
			{
				switch (this.m_dlgMain.DimensionStyleGraph)
				{
				case DimensionStyle.DS_ONE:
					result = "高程点_普通标高.rfa";
					break;
				case DimensionStyle.DS_TWO:
					result = "高程点_普通标高-带基线.rfa";
					break;
				case DimensionStyle.DS_THREE:
					result = "高程点_普通标高-带引线.rfa";
					break;
				case DimensionStyle.DS_FOUR:
					result = "高程点_普通标高-带基线-带引线.rfa";
					break;
				case DimensionStyle.DS_FIVE:
					result = "高程点_三角形室外标高.rfa";
					break;
				case DimensionStyle.DS_SIX:
					result = "高程点_圆点室外标高.rfa";
					break;
				}
			}
			else
			{
				switch (this.m_dlgMain.DimensionStyleGraph)
				{
				case DimensionStyle.DS_ONE:
					if (this.m_bLeft)
					{
						if (this.m_dlgMain.HeaderStyle == 0)
						{
							result = "符号_普通标高(左上).rfa";
						}
						else
						{
							result = "符号_普通标高(左下).rfa";
						}
					}
					else if (this.m_dlgMain.HeaderStyle == 0)
					{
						result = "符号_普通标高(右上).rfa";
					}
					else
					{
						result = "符号_普通标高(右下).rfa";
					}
					break;
				case DimensionStyle.DS_TWO:
					if (this.m_bLeft)
					{
						if (this.m_dlgMain.HeaderStyle == 0)
						{
							result = "符号_普通标高-带基线(左上).rfa";
						}
						else
						{
							result = "符号_普通标高-带基线(左下).rfa";
						}
					}
					else if (this.m_dlgMain.HeaderStyle == 0)
					{
						result = "符号_普通标高-带基线(右上).rfa";
					}
					else
					{
						result = "符号_普通标高-带基线(右下).rfa";
					}
					break;
				case DimensionStyle.DS_THREE:
					if (this.m_bLeft)
					{
						if (this.m_dlgMain.HeaderStyle == 0)
						{
							result = "符号_普通标高-带引线(左上).rfa";
						}
						else
						{
							result = "符号_普通标高-带引线(左下).rfa";
						}
					}
					else if (this.m_dlgMain.HeaderStyle == 0)
					{
						result = "符号_普通标高-带引线(右上).rfa";
					}
					else
					{
						result = "符号_普通标高-带引线(右下).rfa";
					}
					break;
				case DimensionStyle.DS_FOUR:
					if (this.m_bLeft)
					{
						if (this.m_dlgMain.HeaderStyle == 0)
						{
							result = "符号_普通标高-带基线-带引线(左上).rfa";
						}
						else
						{
							result = "符号_普通标高-带基线-带引线(左下).rfa";
						}
					}
					else if (this.m_dlgMain.HeaderStyle == 0)
					{
						result = "符号_普通标高-带基线-带引线(右上).rfa";
					}
					else
					{
						result = "符号_普通标高-带基线-带引线(右下).rfa";
					}
					break;
				case DimensionStyle.DS_FIVE:
					if (this.m_dlgMain.HeaderStyle == 0)
					{
						result = "符号_三角形室外标高(上).rfa";
					}
					else
					{
						result = "符号_三角形室外标高(下).rfa";
					}
					break;
				case DimensionStyle.DS_SIX:
					if (this.m_dlgMain.HeaderStyle == 0)
					{
						result = "符号_圆点室外标高(上).rfa";
					}
					else
					{
						result = "符号_圆点室外标高(下).rfa";
					}
					break;
				}
			}
			return result;
		}

        private static void GetHVOffsetValue(ref XYZ vecHOffset, ref XYZ vecVOffset, Autodesk.Revit.DB.View view, double dOffsetValue, int nHeaderStyle, bool bLeft = false)
		{
			double num = Common.MMToFeet(dOffsetValue);
			vecHOffset = num * view.RightDirection;
			vecVOffset = num * view.UpDirection;
			if (1 == nHeaderStyle)
			{
				vecVOffset = -vecVOffset;
			}
			if (bLeft)
			{
				vecHOffset = -vecHOffset;
			}
		}

        public static void DimensionStyleGraphOne(ref List<Curve> lstDimCurves, Autodesk.Revit.DB.View view, XYZ ptDimensionInsert, double dOffsetValue, int nHeaderStyle, bool bLeft = false)
		{
			XYZ xyz = null;
			XYZ xyz2 = null;
			LevelDimensionManager.GetHVOffsetValue(ref xyz, ref xyz2, view, dOffsetValue, nHeaderStyle, bLeft);
			if (xyz == null && xyz2 == null)
			{
				return;
			}
			XYZ xyz3 = ptDimensionInsert - xyz + xyz2;
			XYZ xyz4 = xyz3 + 2.0 * xyz;
			XYZ xyz5 = xyz4 + 3.0 * xyz;
			Line item = Line.CreateBound(ptDimensionInsert, xyz3);
			Line item2 = Line.CreateBound(ptDimensionInsert, xyz4);
			Line item3 = Line.CreateBound(xyz3, xyz5);
			lstDimCurves.Add(item);
			lstDimCurves.Add(item2);
			lstDimCurves.Add(item3);
		}

        public static void DimensionStyleGraphTwo(ref List<Curve> lstDimCurves, Autodesk.Revit.DB.View view, XYZ ptDimensionInsert, double dOffsetValue, int nHeaderStyle, bool bLeft = false)
		{
			XYZ xyz = null;
			XYZ xyz2 = null;
			LevelDimensionManager.GetHVOffsetValue(ref xyz, ref xyz2, view, dOffsetValue, nHeaderStyle, bLeft);
			if (xyz == null && xyz2 == null)
			{
				return;
			}
			XYZ xyz3 = ptDimensionInsert - xyz + xyz2;
			XYZ xyz4 = xyz3 + 2.0 * xyz;
			XYZ xyz5 = xyz4 + 3.0 * xyz;
			XYZ xyz6 = ptDimensionInsert - 2.0 * xyz;
			XYZ xyz7 = ptDimensionInsert + xyz;
			Line item = Line.CreateBound(ptDimensionInsert, xyz3);
			Line item2 = Line.CreateBound(ptDimensionInsert, xyz4);
			Line item3 = Line.CreateBound(xyz3, xyz5);
			Line item4 = Line.CreateBound(xyz6, xyz7);
			lstDimCurves.Add(item);
			lstDimCurves.Add(item2);
			lstDimCurves.Add(item3);
			lstDimCurves.Add(item4);
		}

        public static void DimensionStyleGraphThree(ref List<Curve> lstDimCurves, Autodesk.Revit.DB.View view, XYZ ptDimensionInsert, double dOffsetValue, int nHeaderStyle, bool bLeft = false)
		{
			XYZ xyz = null;
			XYZ xyz2 = null;
			LevelDimensionManager.GetHVOffsetValue(ref xyz, ref xyz2, view, dOffsetValue, nHeaderStyle, bLeft);
			if (xyz == null && xyz2 == null)
			{
				return;
			}
			XYZ xyz3 = ptDimensionInsert - xyz + xyz2;
			XYZ xyz4 = xyz3 + 2.0 * xyz;
			XYZ xyz5 = ptDimensionInsert + 2.0 * xyz2;
			XYZ xyz6 = xyz5 + 4.0 * xyz;
			Line item = Line.CreateBound(ptDimensionInsert, xyz3);
			Line item2 = Line.CreateBound(ptDimensionInsert, xyz4);
			Line item3 = Line.CreateBound(xyz3, xyz4);
			Line item4 = Line.CreateBound(ptDimensionInsert, xyz5);
			Line item5 = Line.CreateBound(xyz5, xyz6);
			lstDimCurves.Add(item);
			lstDimCurves.Add(item2);
			lstDimCurves.Add(item3);
			lstDimCurves.Add(item4);
			lstDimCurves.Add(item5);
		}

        public static void DimensionStyleGraphFour(ref List<Curve> lstDimCurves, Autodesk.Revit.DB.View view, XYZ ptDimensionInsert, double dOffsetValue, int nHeaderStyle, bool bLeft = false)
		{
			XYZ xyz = null;
			XYZ xyz2 = null;
			LevelDimensionManager.GetHVOffsetValue(ref xyz, ref xyz2, view, dOffsetValue, nHeaderStyle, bLeft);
			if (xyz == null && xyz2 == null)
			{
				return;
			}
			XYZ xyz3 = ptDimensionInsert - xyz + xyz2;
			XYZ xyz4 = xyz3 + 2.0 * xyz;
			XYZ xyz5 = ptDimensionInsert + 2.0 * xyz2;
			XYZ xyz6 = xyz5 + 4.0 * xyz;
			XYZ xyz7 = ptDimensionInsert - 2.0 * xyz;
			XYZ xyz8 = ptDimensionInsert + xyz;
			Line item = Line.CreateBound(ptDimensionInsert, xyz3);
			Line item2 = Line.CreateBound(ptDimensionInsert, xyz4);
			Line item3 = Line.CreateBound(xyz3, xyz4);
			Line item4 = Line.CreateBound(ptDimensionInsert, xyz5);
			Line item5 = Line.CreateBound(xyz5, xyz6);
			Line item6 = Line.CreateBound(xyz7, xyz8);
			lstDimCurves.Add(item);
			lstDimCurves.Add(item2);
			lstDimCurves.Add(item3);
			lstDimCurves.Add(item4);
			lstDimCurves.Add(item5);
			lstDimCurves.Add(item6);
		}

        public static void DimensionStyleGraphFive(ref List<Curve> lstDimCurves, Autodesk.Revit.DB.View view, XYZ ptDimensionInsert, double dOffsetValue, int nHeaderStyle, bool bLeft = false)
		{
			XYZ xyz = null;
			XYZ xyz2 = null;
			LevelDimensionManager.GetHVOffsetValue(ref xyz, ref xyz2, view, dOffsetValue, nHeaderStyle, bLeft);
			if (xyz == null && xyz2 == null)
			{
				return;
			}
			XYZ xyz3 = ptDimensionInsert - xyz + xyz2;
			XYZ xyz4 = xyz3 + 2.0 * xyz;
			Line item = Line.CreateBound(ptDimensionInsert, xyz3);
			Line item2 = Line.CreateBound(xyz3, xyz4);
			Line item3 = Line.CreateBound(xyz4, ptDimensionInsert);
			lstDimCurves.Add(item);
			lstDimCurves.Add(item2);
			lstDimCurves.Add(item3);
		}

        public static void DimensionStyleGraphSix(ref List<Curve> lstDimCurves, Autodesk.Revit.DB.View view, XYZ ptDimensionInsert, double dOffsetValue, int nHeaderStyle, bool bLeft = false)
		{
			double num = Common.MMToFeet(dOffsetValue);
			XYZ xyz = num * view.RightDirection;
			XYZ xyz2 = num * view.UpDirection;
			if (bLeft)
			{
				xyz = -xyz;
			}
			XYZ xyz3 = ptDimensionInsert - xyz;
			XYZ xyz4 = ptDimensionInsert + xyz;
			XYZ xyz5 = ptDimensionInsert + xyz2;
			XYZ xyz6 = ptDimensionInsert - xyz2;
			Arc item = Arc.Create(xyz3, xyz4, xyz5);
			Arc item2 = Arc.Create(xyz4, xyz3, xyz6);
			lstDimCurves.Add(item);
			lstDimCurves.Add(item2);
		}

		public static string FloorLevelByPrecision(string strText, int nPrecisionIndex)
		{
			string result;
			if (nPrecisionIndex == 0)
			{
				result = LevelDimensionManager.StrToDouble(strText, 0.0).ToString("f2");
			}
			else
			{
				result = LevelDimensionManager.StrToDouble(strText, 0.0).ToString("f3");
			}
			return result;
		}

		public static string FloorLevelAddPrefix(string strText)
		{
			if (strText.Contains("±"))
			{
				return strText;
			}
			string text = strText;
			double num = LevelDimensionManager.StrToDouble(strText, 0.0);
			if (num >= -1E-06 && num <= 1E-06)
			{
				text = "±" + text;
			}
			return text;
		}

		public static string FloorInforAddParenthesis(string strText, bool bAddParenthesis)
		{
			if ("" == strText)
			{
				return strText;
			}
			string result = strText;
			if (bAddParenthesis)
			{
				result = "(" + strText + ")";
			}
			return result;
		}

		public static double StrToDouble(string str, double dDef = 0.0)
		{
			double result = dDef;
			double num;
			if (double.TryParse(str, out num))
			{
				result = num;
			}
			return result;
		}

		public static int StrToInt(string str, int nDef = 0)
		{
			int result = nDef;
			int num;
			if (int.TryParse(str, out num))
			{
				result = num;
			}
			return result;
		}

		public static bool StrToBoolean(string str, bool bDef = false)
		{
			bool result = bDef;
			bool flag;
			if (bool.TryParse(str, out flag))
			{
				result = flag;
			}
			return result;
		}

		private ExternalCommandData m_cmdData;

		private Document m_doc;

        private Autodesk.Revit.DB.View m_view;

		private LevelDimensionForm m_dlgMain;

		private XYZ m_ptTextInsert = XYZ.Zero;

		private string m_strDimensionText = "";

		private bool m_bLeft = true;

		private bool m_bViewPlan = true;

		private int m_nRevitNumber = 2015;
	}
}
